//
//  SPMusicFormatToken.m
//  Sophie
//
//  Created by Anton Chebotaev on 19/11/09.
//  Copyright 2009 IFMO. All rights reserved.
//

#import "SPMusicFormatToken.h"

// Constants
NSString * const SPMusicFormatTokenTitleToken = @"##title##";		
NSString * const SPMusicFormatTokenArtistToken = @"##artist##";		
NSString * const SPMusicFormatTokenAlbumToken = @"##album##";		
NSString * const SPMusicFormatTokenRatingToken = @"##raiting##";		
NSString * const SPMusicFormatTokenGenreToken = @"##genre##";		

@implementation SPMusicFormatToken

@synthesize style;

#pragma mark Lifecycle
- (id)init {
	self = [super init];
	if (self) {
        content = [[NSString alloc] init];
        style = NSPlainTextTokenStyle;
    }
    return self;
}
- (id)initWithString:(NSString *)aString {
	self = [super init];
	if (self) {
		content = [aString retain];
		
		if ([content isEqualToString:SPMusicFormatTokenTitleToken] ||
			[content isEqualToString:SPMusicFormatTokenArtistToken] ||
			[content isEqualToString:SPMusicFormatTokenAlbumToken] ||
			[content isEqualToString:SPMusicFormatTokenRatingToken] ||
			[content isEqualToString:SPMusicFormatTokenGenreToken] )
		{
			style = NSRoundedTokenStyle;
		} else {
			style = NSPlainTextTokenStyle;
		}
	}
    return self;	
}
+ (id)tokenWithString:(NSString *)aString {
	SPMusicFormatToken *token = [[[SPMusicFormatToken alloc] initWithString:aString] autorelease];
	return token;
}
- (void)dealloc {
	[content release];
	[super dealloc];
}

+ (id)titleToken {
	return [SPMusicFormatToken tokenWithString:SPMusicFormatTokenTitleToken];
}
+ (id)albumToken {
	return [SPMusicFormatToken tokenWithString:SPMusicFormatTokenAlbumToken];
}
+ (id)artistToken {
	return [SPMusicFormatToken tokenWithString:SPMusicFormatTokenArtistToken];
}
+ (id)genreToken {
	return [SPMusicFormatToken tokenWithString:SPMusicFormatTokenGenreToken];
}
+ (id)ratingToken {
	return [SPMusicFormatToken tokenWithString:SPMusicFormatTokenRatingToken];
}

- (BOOL)isTitleToken {
	return [content isEqual:SPMusicFormatTokenTitleToken];
}
- (BOOL)isAlbumToken {
	return [content isEqual:SPMusicFormatTokenAlbumToken];
}
- (BOOL)isArtistToken {
	return [content isEqual:SPMusicFormatTokenArtistToken];
}
- (BOOL)isGenreToken {
	return [content isEqual:SPMusicFormatTokenGenreToken];
}
- (BOOL)isRatingToken {
	return [content isEqual:SPMusicFormatTokenRatingToken];
}


#pragma mark NSCoding implementation
- (id)initWithCoder:(NSCoder *)aDecoder {
	self = [super init];
	if (self) {
		content = [[aDecoder decodeObjectForKey:@"SPMusicFormatTokenCodingContent"] retain];
		style = [aDecoder decodeIntForKey:@"SPMusicFormatTokenCodingStyle"];
	}
	return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
	[aCoder encodeObject:content forKey:@"SPMusicFormatTokenCodingContent"];
	[aCoder encodeInt:style forKey:@"SPMusicFormatTokenCodingStyle"];
}

#pragma mark NSObject
- (NSString *)description {
	return [NSString stringWithFormat:@"%@/%d", content, style];
}
- (NSUInteger)hash {
	return [[self description] hash];
}
- (BOOL)isEqual:(id)object {
	return [object isMemberOfClass:[SPMusicFormatToken class]] && [[self description] isEqual:[object description]];
}

#pragma mark Handling content
- (NSString *)content {
	if (style == NSPlainTextTokenStyle) {
		return content;
	} else {
		if ([content isEqual:SPMusicFormatTokenTitleToken]) {
			return @"Title";
		} else if ([content isEqual:SPMusicFormatTokenArtistToken]) {
			return @"Artist";
		} else if ([content isEqual:SPMusicFormatTokenAlbumToken]) {
			return @"Album";
		} else if ([content isEqual:SPMusicFormatTokenRatingToken]) {
			return @"Raiting";
		} else if ([content isEqual:SPMusicFormatTokenGenreToken]) {
			return @"Genre";
		} else {
			return [NSString string];
		}		
	}
}

@end
